import WXRealLogUtil from "../../StartAb/WXRealLogUtil";

export default class IconFramesMgr {

    private static mgr: IconFramesMgr  = new IconFramesMgr();
    public static getInstance() {
        return this.mgr;
    }

    private iconAtlasArr: cc.SpriteAtlas[] = [];
    private frameArr = {};

    loadIcons(doneCb) {
        if(this.iconAtlasArr && this.iconAtlasArr.length > 0 && this.iconAtlasArr[0].isValid == true) {
            doneCb && doneCb();
            return;
        }
        cc.assetManager.getBundle("GameAb").loadDir("/lv/icons", cc.SpriteAtlas, (err:Error, atlasArr: cc.SpriteAtlas[])=>{
            if(err) {
                WXRealLogUtil.error("关卡图标加载错误:" + err.message);
                return console.log("IconFramesMgr load err:", err.message)
            }
            atlasArr.forEach((atlas: cc.SpriteAtlas)=>{
                atlas.addRef();
                let lframes = atlas.getSpriteFrames();
                lframes.forEach((sf: cc.SpriteFrame)=>{
                    this.frameArr[sf.name] = sf;
                    sf.addRef();
                })
            })
            this.iconAtlasArr = atlasArr;
            doneCb && doneCb();
        })
    }

    getFrameByName(name:string) {
        return this.frameArr[name]
    }
}
